<?php
    class datcom_model extends CI_Model{
        function __construct(){
            parent::__construct();
            $this->load->database();
        }
        
        function getListNgayDatCom() {
             $query = $this->db->query("(SELECT MaNgayDatCom, NgayDatCom, HanGioDatCom  FROM datcom where NgayDatCom >= Date(NOW()) ) 
                                        union
                                        (SELECT MaNgayDatCom, NgayDatCom, HanGioDatCom  FROM datcom where NgayDatCom > Date(NOW())  
                                        and HanGioDatCom > TIME(NOW()) )
                                        order by NgayDatCom ASC 
                                    ");
            return $query->result_array();
        }
        
        function getListNgayDaDat($mahoso) {
             $query = $this->db->query("SELECT datcom.MaNgayDatCom ,  NgayDatCom   
                            FROM datcom, datcomnhanvien  
                            where 
                            datcom.MaNgayDatCom=datcomnhanvien.MaNgayDatCom
                            and NgayDatCom >=  DATE(NOW())
                            and
                            
                            datcomnhanvien.MaHoSo=".$mahoso." 
                                 
                            order by NgayDatCom ASC 
                                    ");
                            return $query->result_array();
        }
        
        // ham nay loi !!!???
        function num_row(){
            $query = $this->db->query("(SELECT MaNgayDatCom, NgayDatCom, HanGioDatCom  FROM datcom where NgayDatCom = Date(NOW()) ) 
                                        union
                                        (SELECT MaNgayDatCom, NgayDatCom, HanGioDatCom  FROM datcom where NgayDatCom > Date(NOW())  
                                        and HanGioDatCom > TIME(NOW()) )
                                        order by NgayDatCom ASC 
                                    ");
            $num_row=mysql_fetch_row($query);
            $num_row = $query->num_rows;
        }
        
        
        function insertDatCom($mangaydatcom=0, $mahoso=0) {
            $this->db->where(array('MaNgayDatCom' => $mangaydatcom, 'MaHoSo'=>$mahoso));
            $count = $this->db->count_all();
            if($count <= 0){
                $query = $this->db->query(" insert into datcomnhanvien value('".$mangaydatcom."', '".$mahoso."')");    
            }
               
        }
        function deleteDatCom($mangaydatcom=0, $mahoso=0) {
            $query = $this->db->query(" DELETE FROM  datcomnhanvien  WHERE MaNgayDatCom = ".$mangaydatcom." AND MaHoSo  = ".$mahoso);   
        }
        
    }
    
?>